import {observable, action} from 'mobx-miniprogram'
import location from "@src/store/location";
import qqMapSdk from '@src/common/qqmap/map'
import cache from "@src/common/wxCache";
import {getGoodsAndShopJoinPayChannels} from "@src/api/mall/store";
export default observable({
  themeColor: '#03C1B6',
  isiPhoneX: false,
  // 当前选择的定位(用户定位或者选择的地址)
  curLocation: null,
  // 用户定位
  userLocation: null,
  // 用户选择的地址
  chooseAddress: null,
  changeDeliveryType:null,
  storeId: null,
  // actions
  setStoreId: action(function (storeId) {
    this.storeId = storeId;
    cache.set("mall:storeId", storeId, 60 * 60 * 24 * 30)
  }),
  initLocation: action(function (force = false, showTips = true) {
    return new Promise((resolve, reject) => {
      if (!force && this.curLocation) {
        resolve(this.curLocation);
        return;
      }
      location.getLocation(showTips).then(locationRes => {
        this.parseLocation(locationRes).then(res => {
          this.chooseAddress = null;
          this.curLocation = res;
          this.userLocation = res;
          resolve(res);
        }).catch(error => {
          reject(error)
        })
      }).catch((error) => {
        reject(error)
      })
    })
  }),
  setAddress: action(function (address,deliveryType) {
    this.chooseAddress = address;
    this.changeDeliveryType = deliveryType;
    this.curLocation = {
      detail: address.detail,
      latitude: address.latitude,
      longitude: address.longitude,
      city: address.city,
      country: address.country,
      province: address.province,
      street: address.street
    };
    console.log('store里面的this.curLocation', this.curLocation)
  }),
  deteChooseAddress:action(function (){
    this.chooseAddress = null;
  }),
  parseLocation: action(function (locationInfo) {
    return new Promise((resolve, reject) => {
      qqMapSdk.reverseGeocoder({
        location: locationInfo || '', //获取表单传入的位置坐标,不填默认当前位置,示例为string格式
        get_poi: 1, //是否返回周边POI列表：1.返回；0不返回(默认),非必须参数
        success: (res) => {//成功后的回调
          const data = res.result
          let userAddress = {
            addresses: data.pois[0].title || res.address,
            latitude: data.location.lat,
            longitude: data.location.lng,
            city: data.address_component.city,
            country: data.address_component.district,
            province: data.address_component.province,
            street: data.address_component.street
          }
          resolve(userAddress);
        },
        fail: (error) => {
          reject(error)
          console.error("进fail", error);
        },
        complete: (res) => {
        }
      })
    })
  }),
  // 获取商品、门店支付方式交集接口
  goodsAndShopJoinPay: action(function (totalFee,goodsIdList,storeId) {
    console.log('goodsIdList----',goodsIdList)
    return new Promise((resolve, reject) => {
      if(totalFee){
        let idArr = []
        goodsIdList.forEach(el => {
			idArr.push(el.goodsId)
        })
        let parmData = {
			goodsIdList:idArr,
			storeId,
        }
        getGoodsAndShopJoinPayChannels(parmData).then(res => {
          console.log('获取商品、门店支付方式交集接口--res',res)
      	let {supplyBalance,supplyWxPay} = res.data
      	let payCross = supplyBalance || supplyWxPay ? true : false
      	if(!payCross){
      		wx.showModal({
      			title: '温馨提示',
      			content: "当前门店暂无可用支付方式，下单失败，请联系商家处理",
      			confirmText: '我知道了',
      			showCancel: false,
      			success(res) {
      				if (res.confirm) {
      				}
      			}
      		})
      	}
      	resolve(payCross)
        })
      }
    })
  }),
})


